package hfxy.dzx.tx.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;

import hfxy.dzx.tx.model.Circle;

public class TestList {
	public static void main(String []args) {
		List<Circle> list=new ArrayList<>();
		
		//增加数据
		list.add(new Circle(1,1,3));
		list.add(new Circle(1,1,1));
		list.add(new Circle(1,1,2));
		
		//遍历
		System.out.println("直接打印list"+list);
		
		//1
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
		//2
		for(Circle c:list) {
			System.out.println(c);
		}
		
		//3
		ListIterator<Circle> iterator=list.listIterator();
		while(iterator.hasNext()) {
			Circle c=(Circle) iterator.next();			
			System.out.println(c);
		}
		
		//排序
		Collections.sort(list);//必须定义Comparable内比较器，与自己比，故一个参数
		System.out.println("内比较器"+list);
		Collections.sort(list,new OutComp());
		System.out.println("外比较器"+list);
	}

}

class OutComp implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		return (int)( ((Circle)o1).getR()-((Circle)o2).getR() );
		//return 0;
	}
	
}
